package com.project.dao.impl;

import com.project.bean.LinkBean;
import com.project.dao.LinkDao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;

//通讯录持久接口实现类
public class LinkDaoImpl implements LinkDao {
    @Override
    public void add(LinkBean linkBean) {
        Connection con = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            con= DriverManager.getConnection("jdbc:mysql://localhost:6789/mydb?characterEncoding=utf-8",
                    "root","lovo");

            ps = con.prepareStatement("insert into t_link(l_name,l_sex,l_birthday,l_phone)"+"values(?,?,?,?)");

            //填充占位符
            ps.setObject(1,linkBean.getName());
            ps.setObject(2,linkBean.getSex());
            ps.setObject(3,linkBean.getBirthday());
            ps.setObject(4,linkBean.getPhone());
            //更新数据库
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delate(int id) {

    }

    @Override
    public void update(int id, String phone) {

    }

    public static void main(String[] args) {
        LinkDao dao = new LinkDaoImpl();
        dao.add(new LinkBean("小杨","男", LocalDate.parse("1998-10-18"),"18569458562"));
    }
}
